package com.nguyen.service;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.nguyen.application.Common;
import com.nguyen.entities.Category;
import com.nguyen.entities.DishItem;
import com.nguyen.entities.Publisher;
import com.nguyen.entities.UserInfor;

public class Service {
	public static String NAME_SPACE = "http://tempuri.org/";
	public static String FIX_URL = "http://nguyenhuynh.somee.com/LetCookServices/";
	
	@SuppressWarnings("unchecked")
	public boolean checkLogin(String usName, String pwd)
	{	
		String reqUrl = FIX_URL + "LetCookService.asmx?WSDL";
		boolean ret = true;
		try
		{
			SoapObject request = new SoapObject(NAME_SPACE, "doLogin");			
		    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.dotNet = true;
			envelope.bodyOut = request;
			envelope.setOutputSoapObject(request);
			request.addProperty("userId", usName);
			request.addProperty("password", pwd);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(reqUrl);
			
			try 
			{
				androidHttpTransport.call("http://tempuri.org/doLogin", envelope);
				SoapObject result = (SoapObject) envelope.getResponse();
					
				if(result != null)
				{					
					SoapObject obj1 = (SoapObject) result.getProperty(1);				
					if(obj1 != null){
						if(Common.userInfor == null){
							Common.userInfor = new UserInfor();							
						}
						String str = obj1.toString();						
						String usId = str.substring(str.indexOf("userId=")+7, str.indexOf(";", str.indexOf("userId=")));
						String fullName = str.substring(str.indexOf("fullName=")+9, str.indexOf(";", str.indexOf("fullName=")));
						String identityNumber = str.substring(str.indexOf("identityNumber=")+15, str.indexOf(";", str.indexOf("identityNumber=")));
						String email = str.substring(str.indexOf("email=")+6, str.indexOf(";", str.indexOf("email=")));
						Common.userInfor.init(usId, usName, fullName, identityNumber, email);
						ret = true;
					}
						
				}
				else
					ret = false;
			} catch (Exception e) {
				e.printStackTrace();
				ret = false;
			}			
		}
    	catch (Exception e) {
			e.printStackTrace();
			ret = false;
			}
		return ret;
	}
	
	public ArrayList<DishItem> getListDishByCategory(int categoryId, int start, int end){
		ArrayList<DishItem> arrResult = null;
		String reqUrl = FIX_URL + "LetCookService.asmx?WSDL";
		
		try
		{
			SoapObject request = new SoapObject(NAME_SPACE, "getListDishByCategory");			
		    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.dotNet = true;
			envelope.bodyOut = request;
			envelope.setOutputSoapObject(request);
			request.addProperty("categoryId", categoryId);
			request.addProperty("start", start);
			request.addProperty("end", end);
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(reqUrl);
			
			try 
			{
				androidHttpTransport.call("http://tempuri.org/getListDishByCategory", envelope);
				SoapObject result = (SoapObject) envelope.getResponse();
					
				if(result != null)
				{
					arrResult = new ArrayList<DishItem>();
					SoapObject obj1 = (SoapObject) result.getProperty(1);		
					if(obj1 != null){
						SoapObject obj2 = (SoapObject) obj1.getProperty(0);
						int count = obj2.getPropertyCount();
						
						
						for(int i = 0; i < count; i++){
							DishItem dItem = new DishItem();
							Publisher publisher = new Publisher();
							Category category = new Category();
							SoapObject item = (SoapObject)obj2.getProperty(i);
							String str = item.toString();						
							String id = str.substring(str.indexOf("dishId=")+7, str.indexOf(";", str.indexOf("dishId=")));
							String name = str.substring(str.indexOf("dishName=")+9, str.indexOf(";", str.indexOf("dishName=")));
							String price = str.substring(str.indexOf("price=")+6, str.indexOf(";", str.indexOf("price=")));
							String diff = str.substring(str.indexOf("difficulty=")+11, str.indexOf(";", str.indexOf("difficulty=")));
							String time = str.substring(str.indexOf("time=")+5, str.indexOf(";", str.indexOf("time=")));
							String people = str.substring(str.indexOf("people=")+7, str.indexOf(";", str.indexOf("people=")));
							String imageUrl = str.substring(str.indexOf("imageUrl=")+9, str.indexOf(";", str.indexOf("imageUrl=")));
							String description = str.substring(str.indexOf("description=")+12, str.indexOf(";", str.indexOf("description=")));
							
							// publisher
							String pId = str.substring(str.indexOf("publisherId=")+12, str.indexOf(";", str.indexOf("publisherId=")));
							String pAdd = str.substring(str.indexOf("address=")+8, str.indexOf(";", str.indexOf("address=")));
							String pInfor = str.substring(str.indexOf("information=")+12, str.indexOf(";", str.indexOf("information=")));
							String pName = str.substring(str.indexOf("publisherName=")+14, str.indexOf(";", str.indexOf("publisherName=")));
							publisher.init(Integer.parseInt(pId), pName, pAdd, pInfor);
							
							// category
							String cId = str.substring(str.indexOf("categoryId=")+11, str.indexOf(";", str.indexOf("categoryId=")));						
							category.init(Integer.parseInt(cId), "", "", "");
							
							// dish item
							dItem.init(Integer.parseInt(id), name, price, Integer.parseInt(diff), Integer.parseInt(time), Integer.parseInt(people), imageUrl, description, category, publisher);
							
							// add to array
							arrResult.add(dItem);
						}
					}
						
				}
				else
					arrResult = null;
			} catch (Exception e) {
				e.printStackTrace();
				
			}			
		}
    	catch (Exception e) {
			e.printStackTrace();
			
			}
		
		return arrResult;
	}
	
	public ArrayList<Category> getListCategory(){
		ArrayList<Category> arrResult = null;
		String reqUrl = FIX_URL + "LetCookService.asmx?WSDL";
		
		try
		{
			SoapObject request = new SoapObject(NAME_SPACE, "getListCategory");			
		    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.dotNet = true;
			envelope.bodyOut = request;
			envelope.setOutputSoapObject(request);
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(reqUrl);
			
			try 
			{
				androidHttpTransport.call("http://tempuri.org/getListCategory", envelope);
				SoapObject result = (SoapObject) envelope.getResponse();
					
				if(result != null)
				{
					arrResult = new ArrayList<Category>();
					SoapObject obj1 = (SoapObject) result.getProperty(1);		
					if(obj1 != null){
						SoapObject obj2 = (SoapObject) obj1.getProperty(0);
						int count = obj2.getPropertyCount();						
						
						for(int i = 0; i < count; i++){
							
							Category category = new Category();
							SoapObject item = (SoapObject)obj2.getProperty(i);
							String str = item.toString();						
														
							// category
							String cId = str.substring(str.indexOf("categoryId=")+11, str.indexOf(";", str.indexOf("categoryId=")));
							String cTitle = str.substring(str.indexOf("title=")+6, str.indexOf(";", str.indexOf("title=")));
							String cDesc = str.substring(str.indexOf("imageUrl=")+9, str.indexOf(";", str.indexOf("imageUrl=")));
							String cImgUrl = str.substring(str.indexOf("description=")+12, str.indexOf(";", str.indexOf("description=")));
							category.init(Integer.parseInt(cId), cTitle, cDesc, cImgUrl);
														
							// add to array
							arrResult.add(category);
						}
					}
						
				}
				else
					arrResult = null;
			} catch (Exception e) {
				e.printStackTrace();
				
			}			
		}
    	catch (Exception e) {
			e.printStackTrace();
			
			}
		
		return arrResult;
	}
}
